//
//  YYJLNewCurriculumController.m
//  YuanyangJinlou
//
//  Created by mac004 on 2017/1/10.
//  Copyright © 2017年 suli19910309@qq.com. All rights reserved.
//

#import "YYJLNewCurriculumController.h"
#import "YYJLCurriculumDetailViewController.h"
@interface YYJLNewCurriculumController ()<ChangeViewDelegate,UIScrollViewDelegate>
{
    ChangeView      *_tabbarView;      //分栏
    NSInteger                               selectTabbarIndex; //分栏选中
    NSMutableArray                          *_ViewCtrlArr;
    UIScrollView                            *_scrollView;
}
@end

@implementation YYJLNewCurriculumController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitleView:@"最新课程"];
    [self setBackItem];
   
    [self creatUI];
    [self addController];

}
-(void)creatUI{
    _ViewCtrlArr = [[NSMutableArray alloc]initWithObjects:@"视频课程",@"图文资料", nil];
    _tabbarView = [[ChangeView alloc]initWithFrame:CGRectMake(0, NAV_HEIGHT, SCREEN_WIDTH, 44*PER_HEIGHT) withCount:_ViewCtrlArr withbgColor:BLACK_COLOR withSelectColor:TINK_COLOR];
    _tabbarView.delegate = self;
    [self.view addSubview:_tabbarView];
    
    //全局滚动
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, _tabbarView.bottom, SCREEN_WIDTH, self.view.height-_tabbarView.bottom)];
    _scrollView.backgroundColor = [UIColor clearColor];
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.bounces = YES;
    [self.view addSubview:_scrollView];
    _scrollView.contentSize = CGSizeMake(_scrollView.width*_ViewCtrlArr.count, _scrollView.height);
    
    
}
//添加子控件
- (void)addController
{
    for (int i=0; i<_ViewCtrlArr.count; i++) {
        YYJLCurriculumDetailViewController *show = [[YYJLCurriculumDetailViewController alloc]init];
        if (i==0) {
            show.style  = VideoType;
        }
        else if (i==1) {
            show.style = ITtype;
        }
        
        [self addChildViewController:show];
        
        if (i == 0) {
            show.view.frame = CGRectMake(0, 0, _scrollView.width, _scrollView.height);
            [_scrollView addSubview:show.view];
        }
    }
}
- (void)onSelectItemForView:(ChangeView *)view withIndex:(NSInteger)selectIndex
{
    
  
    

        NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
        if (index != selectIndex) {
            selectTabbarIndex = selectIndex;
            YYJLCurriculumDetailViewController *show = [[YYJLCurriculumDetailViewController alloc]init];
            
            if (selectIndex==0) {
                show.style  = VideoType;
            }else{
                show.style = ITtype;
            }
            [_scrollView setContentOffset:CGPointMake(selectIndex*_scrollView.width, 0) animated:YES];
    
    
    
        }
}
#pragma mark - 滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
    if (index != selectTabbarIndex) {
        selectTabbarIndex = index;
        [_tabbarView onUPdateUIForSelectIndex:index];
        
        [self scrollViewDidEndScrollingAnimation:_scrollView];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
    YYJLCurriculumDetailViewController *show = self.childViewControllers[index];
    if (show.view.superview) {
        return;
    }
    show.view.frame = scrollView.bounds;
    [_scrollView addSubview:show.view];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
